#include "stringoperations.h"

#include <algorithm>

using namespace std;

void StringOperations::lowercase(string& aString)
{
    transform(aString.begin(), aString.end(), aString.begin(), LowerCase());
}

void StringOperations::dos2unix(string& line)
{
    replace(line, "\r\n", "\n");
}

void StringOperations::replace(string& aString, const string& toSearch, const string& toReplace)
{
    size_t startPos = 0;
    size_t foundPos;
    
    while (string::npos != (foundPos = aString.find(toSearch, startPos)))
    {
        aString.replace(foundPos, toSearch.length(), toReplace);
        startPos = foundPos + toReplace.size();
    }
}

void StringOperations::tokenize(const string& str, const string& delimiter, vector<string>& tokens)
{
    string          tempString = str;
    size_t          pos = 0;
    
    tokens.clear();
    while ((pos = tempString.find(delimiter)) != string::npos)
    {
        tokens.push_back(tempString.substr(0, pos));
        tempString.erase(0, pos + delimiter.size());
    }
	
	tokens.push_back(tempString);
}

void StringOperations::trim(string& str)
{
	size_t begin 	= str.find_first_not_of(" \n\t");
	size_t end 		= str.find_last_not_of(" \n\t");
	
	str = str.substr(begin, end);
}
